<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * 用户记录
 */
class UserDevice extends Model
{
    protected $table = 'user_device';
    protected $fillable = ['user_id', 'device_id', 'status'];

    const STATUS_OK = 1;
    const STATUS_FREEZE = 0;

    /**
     * 绑定用户，设备记录
     */
    static function addRecord($userId = 0, $deviceId = '')
    {
        if (!$userId || !$deviceId) return false;
        $info = [
            'user_id' => $userId,
            'device_id' => $deviceId,
        ];
        return self::updateOrCreate($info, $info);
    }

    /**
     * 封禁账号相关的设备
     */
    static function freezeUserDevice($userId = 0)
    {
        return self::where('user_id', $userId)->update(['status' => self::STATUS_FREEZE]);
    }

    /**
     * 查询设备冻结情况
     */
    static function checkDeviceFreeze($deviceId = '')
    {
        if (!$deviceId) return false;
        $where = [
            'device_id' => $deviceId,
            'status' => self::STATUS_FREEZE
        ];
        return self::where($where)->count() > 0 ? true : false;
    }
}
